package com.wutong.teacher_server.utils;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Date工具类
 * @author pj
 * 2018-11-07 20:35
 *
 */
public class DateUtil {

	public static SimpleDateFormat getYyyMMddHHmmss() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}

	public static SimpleDateFormat getYyyMMdd() {
		return new SimpleDateFormat("yyyy-MM-dd");
	}

	public static SimpleDateFormat gethHmmss() {
		return new SimpleDateFormat("HH:mm:ss");
	}

	public static String dateToStr(Date date ) {
		return dateToStr(date , "");
	}
	/**
	 * date转String【yyyy-MM-dd HH:mm:ss】
	 * @param date 时间
	 * @author pj
	 * 2018-11-07 20:35
	 * @return
	 */
	public static String dateToStr(Date date ,String timeZone) {
		if(!StringUtils.isEmpty(timeZone)){
			getYyyMMddHHmmss().setTimeZone(TimeZone.getTimeZone(timeZone)); //设置时区
		}
		return getYyyMMddHHmmss().format(date);
	}

	/**
	 * 判断当前日期是星期几
	 * @param pTime 需要判断的时间
	 * @return dayForWeek 判断结果：日历对象中星期几对应的数值
	 * @author pj
	 * 2018-11-07 20:37
	 * @throws Exception
	 */
	public static int dayForWeekValue(String pTime) {
		if(StringUtils.isEmpty(pTime)){
			pTime=dateToStr(new Date());
		}
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(getYyyMMdd().parse(pTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if(c.get(Calendar.DAY_OF_WEEK) == 1){
			dayForWeek = 7;
		}else{
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		//		dayForWeek = c.get(Calendar.DAY_OF_WEEK);
		return dayForWeek;
	}


	/**
	 * Date  返回  String类型  yyyy-MM-dd HH:mm:ss格式
	 * @return String
	 */
	public static Date Stringtodate(String date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			//	date = (date.length()>22)?date.substring(0, 22):date;
			return  format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

	}
	/**
	 * 得到一个时间延后或前移几天的时间
	 * @param reqDate 时间对象
	 * @param delay 前移或后延的天数
	 * @author pj
	 * 2018-11-08 09:35
	 * @return yyyy-MM-dd
	 */
	public static String getNextDay(Date reqDate, String delay) {
		try {
			long myTime = (reqDate.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
			Date date = new Date();
			date.setTime(myTime * 1000);
			return getYyyMMdd().format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 得到一个时间延后或前移几天的时间
	 * @param reqDate 时间对象
	 * @param delay 前移或后延的天数
	 * @author pj
	 * 2018-11-08 10:04
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNextDayTo2(Date reqDate, String delay) {
		try {
			long myTime = (reqDate.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
			Date date = new Date();
			date.setTime(myTime * 1000);
			return date;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取一年后date
	 * @return
	 */
	public static Date getNextYear() {
		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1);
		return curr.getTime();
	}

	/**
	 * date转String【yyyy-MM-dd】
	 * @param date 时间
	 * @author pj
	 * 2018-11-08 14:45
	 * @return
	 */
	public static String dateToStrToyyMMdd(Date date) {
		return getYyyMMdd().format(date);
	}

	public static Long getEndTimeSec() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 10000);
		String time = Long.toString(calendar.getTimeInMillis());
		String newTime = time.substring(0, time.length()-3);
		long l = Long.parseLong(newTime);
		return l;
	}

	/**
	 * date转String【yyyy-MM-dd】
	 * @param
	 * @return
	 */
	public static String getToday() {
		Date d = new Date();
		//创建一个格式化对象
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		//格式化为日期/时间字符串
		String cc = sdf.format(d);
		return cc;
	}



	  public static boolean judgeTime(String  nowTime) throws ParseException {
		  Date nowTime1 = gethHmmss().parse(nowTime);
	        Date startTime = gethHmmss().parse("00:01:00");
	        Date endTime = gethHmmss().parse("06:00:00");
	        return isEffectiveDate(nowTime1, startTime, endTime);
	  }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

	/**
	 * 比较两个时间大小 HH:mm:ss
	 * @param timeOne
	 * @param timeTwo
	 * @return
	 */
    public static boolean eqTime(Date timeOne,Date timeTwo){
//		SimpleDateFormat format = new SimpleDateFormat("hhmmss");
		if (timeOne.before(timeTwo)) {
			return false;
		}
		return true;
//		int o=Integer.parseInt(format.format(timeOne).toString());
//    	int t=Integer.parseInt(format.format(timeTwo).toString());
//    	if(o>t){
//    		return true;
//		}
//    	return false;
	}

	public static void main(String[] args) throws ParseException {
		String time="19:13:13";
		String time1="19:13:13";
		Date t = DateUtil.gethHmmss().parse(time);
		Date t1= DateUtil.gethHmmss().parse(time1);
		System.out.println(eqTime(t, t1));
	}

	/**
	 * 传入时间向前或向后推m秒
	 * @param date
	 * @param m
	 * @return
	 */
	public static Date getTime(String date,int m) throws ParseException {
		if (StringUtils.isEmpty(date)) {
			return null;
		}
		String[] split = date.split(":");
		Calendar c = Calendar.getInstance();
		Date parse = gethHmmss().parse(date);
		c.set(Calendar.HOUR_OF_DAY,Integer.parseInt(split[0]));
		c.set(Calendar.MINUTE,Integer.parseInt(split[1]));
		c.set(Calendar.SECOND,Integer.parseInt(split[2]));
		c.add(Calendar.SECOND,m);
		Date time = c.getTime();
//		Date date1 = new Date();
		return time;
	}
}